package com.nas.crocus.dao.impl;

import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.nas.crocus.dao.HotelDao;
import com.nas.crocus.dao.PersonaDao;
import com.nas.crocus.domain.Evento;
import com.nas.crocus.domain.Hotel;
import com.nas.crocus.domain.Persona;

@Repository
public class PersonaDaoImp implements PersonaDao{

    @Autowired
    private SessionFactory sessionFactory;
    
    @Override
    public Persona buscar(Long alojamientoId) {
        return (Persona) sessionFactory.getCurrentSession().get(Persona.class,
                alojamientoId);
    }

    @Override
    public Collection<Persona> buscar() {
        Session session = sessionFactory.getCurrentSession();        
        return session.createCriteria(Persona.class).list();
    }

    @Override
    public void guardar(Persona persona) {
        sessionFactory.getCurrentSession().save(persona);  
        
    }

    @Override
    public void actualizar(Persona persona) {
        sessionFactory.getCurrentSession().update(persona);  
    }

    @Override
    public Collection<Persona> buscarPorPatronApellidoNombre(String patron) {
        String queryPatron = patron + "%";
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(Persona.class);
        Criterion apellido = Restrictions.like("apellido", queryPatron);
        Criterion nombre = Restrictions.like("nombre", queryPatron);
        Disjunction disjunction = Restrictions.disjunction();
        disjunction.add(apellido);
        disjunction.add(nombre);
        criteria.add(disjunction);
        
        return criteria.list();
    }

    @Override
    public Collection<Persona> buscarPorPatronNumeroDocumento(Integer patron) {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Persona persona where persona.numeroDoc like '" + patron + "%'");
        return query.list();
    }

   
    
}
